package com.culturalCenter.dataCenter.Utils;

import com.culturalCenter.dataCenter.mapper.base.BaseMapper;

import java.util.List;

/**** 数据库数据筛选类
 * @Author:www.rookie.com
 * @Date 2020/8/25 17:48
 *****/
public class MapperUtil<T> {

    private static MapperUtil instance;

    private MapperUtil() {

    }

    public static MapperUtil getInstance() {
        if (instance == null) {
            instance = new MapperUtil();
        }
        return instance;
    }

    /**
     * 获取一条记录
     */
    public T getOneResult(BaseMapper<T> mapper) {
        List<T> list = (List<T>) mapper.findPageAll();
        switch (list.size()) {
            case 0:
                return null;
            case 1:
                return list.get(0);
            default:
                throw new RuntimeException("表中不是1条数据");
        }
    }

    /**
     * 通过实体类获取一条记录
     */
    public T getOneResultByClass(BaseMapper<T> mapper, Object o) {
        List<T> list = (List<T>) mapper.queryAll((T) o);
        switch (list.size()) {
            case 0:
                return null;
            case 1:
                return list.get(0);
            default:
                throw new RuntimeException("表中不是1条数据");
        }
    }

    /**
     * 通过用户id获取一条记录
     */
    public T getOneResult(BaseMapper<T> mapper, String userId) {
        List<T> list = mapper.queryByUserId(userId);
        switch (list.size()) {
            case 0:
                return null;
            case 1:
                return list.get(0);
            default:
                throw new RuntimeException("表中不是1条数据");
        }

    }

}
